﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace ZhonTai.Industry.Core.Tool
{
    public static class JsonHelpers
    {
        public static bool IsTrueValue(this JsonElement element) =>
            element.ValueKind switch
            {
                JsonValueKind.True => true,
                JsonValueKind.Number => element.TryGetDecimal(out decimal num) && num != 0,
                JsonValueKind.String => IsTrueString(element),
                _ => false
            };

        public static bool IsTrueString(this JsonElement element) =>
            element.ValueKind == JsonValueKind.String &&
            element.GetString().AsSpan().Equals("true".AsSpan(), StringComparison.OrdinalIgnoreCase);

        public static bool IsFalseString(this JsonElement element) =>
            element.ValueKind == JsonValueKind.String &&
            (element.GetString().AsSpan().Equals("false".AsSpan(), StringComparison.OrdinalIgnoreCase) ||
             element.GetString().AsSpan().Equals("0".AsSpan(), StringComparison.Ordinal));
    }
}